package xcmd

import (
	"os"
	"strings"

	"gitee.com/xiaoyutab/xgotool/xerror"
)

// 获取指定目录内的文件列表
func Find(path string, arg ...string) ([]string, error) {
	if is_windows {
		return nil, xerror.New("抱歉，此命令暂不支持运行在Windows平台下")
	}
	if path == "" {
		return nil, xerror.New("目录不允许为空")
	}
	if path[len(path)-1] != '/' {
		path += "/"
	}
	if _, err := os.Stat(path); err != nil {
		return nil, err
	}
	arg = append([]string{path}, arg...)
	inf, err := Exec(cmd_check[FIND].Path, arg...)
	if err != nil {
		return nil, err
	}
	rets := []string{}
	pam := strings.Split(inf, "\n")
	for i := 0; i < len(pam); i++ {
		if pam[i] == path || pam[i] == "" {
			continue
		}
		rets = append(rets, pam[i][len(path):])
	}
	return rets, nil
}
